package com.tesis_2.DA;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tesis_2.MO.MOLocal;
import com.tesis_2.MO.MORestaurant;

public class DAOLocales {

	public static ArrayList<MOLocal> getLocales() throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		ArrayList<MOLocal> lsLocales = new ArrayList<MOLocal>();
		
	    try{

	    	JSONArray locales = (new JSONReaderArray().execute(DAOUtils.getServer()+ "/locals.json")).get();
	    	
	    	if (locales == null) return lsLocales;
	       	    	
	    	for (int i = 0; i < locales.length(); i++) {
	    		JSONObject jsonlocales = locales.getJSONObject(i);
	    	    jsonlocales = jsonlocales.getJSONObject("local");
	    	    //obtengo el restaurante
	    	    MOLocal objLocal = new MOLocal();
	    	    //entro al objeto y leo los atributos
	    	    objLocal.setName(jsonlocales.getString("name"));
	    	    objLocal.setId(jsonlocales.getInt("id")) ;
	    	    objLocal.setDescr(jsonlocales.getString("descr"));
	    	    objLocal.setAddr1(jsonlocales.getString("addr1"));
	    	    objLocal.setAddr2(jsonlocales.getString("addrs2"));
	    	    objLocal.setPhone1(jsonlocales.getString("phone1"));
	    	    objLocal.setPhone2(jsonlocales.getString("phone2"));
	    	    objLocal.setCapacity(jsonlocales.getInt("capacity"));
	    	    objLocal.setRest_id(jsonlocales.getInt("restaurant_id"));
	    	    objLocal.setStatus(jsonlocales.getInt("status"));
	    	    objLocal.setRate((float) (jsonlocales.getDouble("rate")));
	    	    objLocal.setUpdated_at(DAOUtils.parseDateIso(jsonlocales.getString("updated_at")));
	    	    objLocal.setCreated_at(DAOUtils.parseDateIso(jsonlocales.getString("created_at")));
	    	    
	    	  //objLocal.setPhoto(jsonlocales.getString("photo_file_name"));
	    	    //{"local":{"addr1":"Address1","addrs2":"Address2","capacity":20,"created_at":"2012-12-10T02:48:59Z",
	    	    //"descr":"Centro de Lima","id":1,"name":"Centro","phone1":"123465","phone2":"78946",
	    	    //"restaurant_id":1,"status":1,"updated_at":"2012-12-10T02:49:22Z"}}
	    	    
	    	    lsLocales.add(objLocal);
	    	}
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsLocales;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsLocales;
		}
		return lsLocales;
		
	}
	
}
